package com.csu.express.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csu.common.constants.CommonParams;
import com.csu.common.exception.WebException;
import com.csu.common.sms.SMSSender;
import com.csu.common.utils.AuthCodeUtil;
import com.csu.common.utils.RedisUtil;
import com.csu.express.constants.ErrorCode;
import com.csu.express.constants.RedisKey;
import com.csu.express.dao.OrderMapper;
import com.csu.express.pojo.OrderPojo;
import com.csu.express.service.CommonService;
import com.csu.express.vo.HeartbeatMsgVo;
import com.csu.express.vo.MsgBean;

/**
 * @项目名称：express
 * @类名称：CommonServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年9月22日 上午8:59:27
 * @company:
 */
@Service
public class CommonServiceImpl implements CommonService
{
    @Autowired
    private OrderMapper orderMapper;

    /**
     * @see com.csu.express.service.CommonService#sendSignCode(java.lang.String)
     */
    @Override
    public Boolean sendSignCode(String cellphone)
    {
        String key = RedisKey.Sign_Auth_Code + cellphone;
        String authCode = AuthCodeUtil.getCode(4);//4位验证码
        RedisUtil.getRedisDb().setex(key, CommonParams.Sign_Auth_Code_Time, authCode);
        return SMSSender.sendSignSMS(cellphone, authCode);
    }

    /**
     * @throws WebException
     * @see com.csu.express.service.CommonService#checkAuthCode(java.lang.String,
     *      java.lang.String)
     */
    @Override
    public Boolean checkAuthCode(String cellphone, String authCode) throws WebException
    {
        String key = RedisKey.Sign_Auth_Code + cellphone;
        String redisCode = RedisUtil.getRedisDb().get(key);
        if (authCode.equals(redisCode))
        {
            return true;
        }
        throw new WebException(ErrorCode.AuthCodeError, ErrorCode.AuthCodeErrorMessgae);
    }

    /**
     * @see com.csu.express.service.CommonService#heartbeat(java.lang.Integer,
     *      java.lang.Integer)
     */
    @Override
    public List<HeartbeatMsgVo> heartbeat(Integer id, Integer userType)
    {
        // TODO
        if (userType == 2)
        {
            List<HeartbeatMsgVo> msgs =new ArrayList<HeartbeatMsgVo>();
            HeartbeatMsgVo msg = new HeartbeatMsgVo();
            
            MsgBean msgBean = new MsgBean();
            msgBean.setBeanId("bf63335b793746d8ba0215b00400e25a");
            
            OrderPojo order = orderMapper.selectBySn("bf63335b793746d8ba0215b00400e25a");
            msgBean.setContent(order);
            msgBean.setFuncType(1);
            msgBean.setTitle("有新的订单分配给您");
            
            msg.setMsgType(1);
            msg.setMsgBean(msgBean);
            
            msgs.add(msg);
            return msgs;
        }
        return null;
    }

}
